﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
using System.Collections;

namespace Calc
{
    public partial class frmMainTab : Form
    {
       
        public frmMainTab()
        {
            InitializeComponent();

            
        }
        public void Activate()
        {
            this.Refresh();
            BindDatatoWorkSheet();
            
        }
        ManageItems objManageItems = new ManageItems();
        Entities objEntities = new Entities();
        ProjectDB objProjectDB = new ProjectDB();
        ProjectEntities objProjectEntities = new ProjectEntities();
        int GroupID1 = 0;
        int ItemID1 = 0;
        int AssemblyID1 = 0;
        int CheckedItemID = -1;
        int CheckedGroupID = -1;
        int CheckedGroupIDAss = -1;
        int CheckedAssemblyID = -1;
        decimal MiscItemCostTotal = 0;
        int DataBinding = 0;
        private void frmMainTab_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'pubsDataSet.authors' table. You can move, or remove it, as needed.
            CheckDatabase();
            GetGroups();
            GetItems();
            GetAssemblies();
            GetMaterialType();
            txtAddGroup.Visible = false;
            btnSaveGroup.Visible = false;
            btnCancel.Visible = false;
            DataSet ds = new DataSet();
            objProjectEntities.ProjectID = 0;
            ds = objProjectDB.GetProjectDetails(objProjectEntities);
            GetProjectDetails(ds);
        }
        private void CheckDatabase()
        {
            CheckDb obj = new CheckDb();
            obj.CheckDB();
        }   
        private void GetMaterialType()
        {
            DataSet ds = new DataSet();
            objEntities.Action = 0;
            ds = objManageItems.GetMaterialType(objEntities);

            if (ds.Tables[0].Rows.Count > 0)
            {
                ddlMaterialType.DataSource = ds.Tables[0];
                ddlMaterialType.DisplayMember = "MaterialName";
                ddlMaterialType.ValueMember = "MaterialTypeID";
            }
        }
        private void GetAssemblies()
        {
            CheckedAssemblyID = -1;
            listAssemblies.Items.Clear();
            DataSet ds = new DataSet();
            objEntities.AssemblyID = 0;
            objEntities.AssemblyName = "";
            objEntities.Action = 0;
            ds = objManageItems.GetAssemblies(objEntities);

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                ListViewItem lv = new ListViewItem(ds.Tables[0].Rows[i]["AssemblyName"].ToString());
                lv.SubItems.Add(ds.Tables[0].Rows[i]["AssemblyID"].ToString());
                listAssemblies.Items.Add(lv);
            }
            //listGroups.Items.Insert(0, "All Groups");
            //listGroupsAss.Items.Insert(0, "All Groups");
        }
        private void GetGroups()
        {
            CheckedGroupID = -1;
            CheckedGroupIDAss = -1;
            listGroups.Items.Clear();
            listGroupsAss.Items.Clear();
            DataSet ds = new DataSet();
            objEntities.GroupID = 0;
            objEntities.GroupName = "";
            objEntities.Action = 0;
            ds = objManageItems.GetGroups(objEntities);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i]["GroupName"].ToString() != "(none)")
                {
                    ListViewItem lv = new ListViewItem(ds.Tables[0].Rows[i]["GroupName"].ToString());
                    ListViewItem lv1 = new ListViewItem(ds.Tables[0].Rows[i]["GroupName"].ToString());
                    lv.SubItems.Add(ds.Tables[0].Rows[i]["GroupID"].ToString());
                    lv1.SubItems.Add(ds.Tables[0].Rows[i]["GroupID"].ToString());
                    listGroups.Items.Add(lv);
                    listGroupsAss.Items.Add(lv1);
                }
            }
            listGroups.Items.Insert(0, "All Groups");
            listGroupsAss.Items.Insert(0, "All Groups");
            
            //listGroups.Items[0].Checked = true;

            ddlGroup.DataSource = ds.Tables[0];
            ddlGroup.DisplayMember = "GroupName";
            ddlGroup.ValueMember = "GroupID";
            ddlGroup.SelectedValue = 4;
        }
        private void GetItems()
        {
            CheckedItemID = -1;
            listItems.Items.Clear();
            listItemsAss.Items.Clear();
            DataSet ds = new DataSet();
            objEntities.Action = 0;
            objEntities.ItemID = 0;
            objEntities.GroupID = 0;
            ds = objManageItems.GetItems(objEntities);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                ListViewItem lv = new ListViewItem(ds.Tables[0].Rows[i]["ItemName"].ToString());
                ListViewItem lv1 = new ListViewItem(ds.Tables[0].Rows[i]["ItemName"].ToString());
                lv.SubItems.Add(ds.Tables[0].Rows[i]["MaterialName"].ToString());
                lv1.SubItems.Add(ds.Tables[0].Rows[i]["MaterialName"].ToString());
                lv.SubItems.Add(ds.Tables[0].Rows[i]["GroupName"].ToString());
                lv1.SubItems.Add(ds.Tables[0].Rows[i]["GroupName"].ToString());
                lv.SubItems.Add(ds.Tables[0].Rows[i]["ItemID"].ToString());
                lv1.SubItems.Add(ds.Tables[0].Rows[i]["ItemID"].ToString());
                listItems.Items.Add(lv);
                listItemsAss.Items.Add(lv1);
            }   

        }
        private void GetItemsByID(int ItemID)
        {
            
            objEntities.Action = 1;
            objEntities.ItemID = ItemID;
            objEntities.GroupID = 0;
            DataSet ds = new DataSet();
            ds = objManageItems.GetItems(objEntities);
            if (ds.Tables[0].Rows.Count > 0)
            {
                ItemID1 = Convert.ToInt16(ds.Tables[0].Rows[0]["ItemID"].ToString());
                txtItemName.Text = ds.Tables[0].Rows[0]["ItemName"].ToString();
                ddlGroup.SelectedValue = Convert.ToInt16(ds.Tables[0].Rows[0]["GroupID"].ToString());
                ddlMaterialType.SelectedValue = Convert.ToInt16(ds.Tables[0].Rows[0]["MaterialTypeID"].ToString());
                txtCost.Text = ds.Tables[0].Rows[0]["Cost"].ToString();
                ddlCostPer.SelectedValue = ds.Tables[0].Rows[0]["CostPer"].ToString();
                txtCoverage.Text = ds.Tables[0].Rows[0]["Coverage"].ToString();
                txtWaste.Text = ds.Tables[0].Rows[0]["Waste"].ToString();
                txtLabour.Text = ds.Tables[0].Rows[0]["Labour"].ToString();
            }
            else
            {
                ItemID1 = 0;
                txtItemName.Text = "";
                ddlGroup.SelectedIndex = 0;
                ddlMaterialType.SelectedIndex = 2;
                txtCost.Text = "";
                ddlCostPer.SelectedIndex = -1;
                txtCoverage.Text = "";
                txtWaste.Text = "";
                txtLabour.Text = "";
            }
        }
        private void GetItemsByGroup(int GroupID)
        {
            CheckedItemID = -1;
            //CheckedGroupID=-1;
            listItems.Items.Clear();
            listItemsAss.Items.Clear();
            DataSet ds = new DataSet();
            if (GroupID != 0)
                objEntities.Action = 2;
            else
                objEntities.Action = 0;
            objEntities.ItemID = 0;
            objEntities.GroupID = GroupID;
            ds = objManageItems.GetItems(objEntities);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                ListViewItem lv = new ListViewItem(ds.Tables[0].Rows[i]["ItemName"].ToString());
                ListViewItem lv1 = new ListViewItem(ds.Tables[0].Rows[i]["ItemName"].ToString());
                lv.SubItems.Add(ds.Tables[0].Rows[i]["MaterialName"].ToString());
                lv1.SubItems.Add(ds.Tables[0].Rows[i]["MaterialName"].ToString());
                lv.SubItems.Add(ds.Tables[0].Rows[i]["GroupName"].ToString());
                lv1.SubItems.Add(ds.Tables[0].Rows[i]["GroupName"].ToString());
                lv.SubItems.Add(ds.Tables[0].Rows[i]["ItemID"].ToString());
                lv1.SubItems.Add(ds.Tables[0].Rows[i]["ItemID"].ToString());
                listItems.Items.Add(lv);
                listItemsAss.Items.Add(lv1);
            }   
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
        {
            MessageBox.Show("asdf");
        }

        private void listItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection selectedItemsCollection = this.listItems.SelectedItems;
            if(selectedItemsCollection.Count > 0)
            {
                CheckedItemID = selectedItemsCollection[0].Index;
                if (CheckedItemID != -1)
                    listItems.Items[CheckedItemID].Checked = false;
                if (listItems.SelectedItems[0].Selected)
                {
                    CheckedItemID = Convert.ToInt16(CheckedItemID);
                    txtItemName.Enabled = true;
                    ddlGroup.Enabled = true;
                    ddlMaterialType.Enabled = true;
                    txtCost.Enabled = true;
                    ddlCostPer.Enabled = true;
                    txtCoverage.Enabled = true;
                    txtWaste.Enabled = true;
                    txtLabour.Enabled = true;
                    groupBox1.Enabled = true;
                    btnSaveItem.Enabled = true;
                    GetItemsByID(Convert.ToInt16(listItems.SelectedItems[0].SubItems[3].Text));
                }
                Console.WriteLine(listItems.SelectedItems[0].Index);
            }
        }
    private void listAssemblies_ItemChecked(object sender, ItemCheckedEventArgs e)
    {
        if (CheckedAssemblyID != -1)
        {
            listAssemblies.Items[CheckedAssemblyID].Checked = false;
        }
        if(e.Item.Checked==true)
        {
            objEntities.AssemblyID = Convert.ToInt16(e.Item.SubItems[1].Text);
            DataSet ds = new DataSet();
            ds = objManageItems.GetItemsByAssemblies(objEntities);
            CheckedAssemblyID = e.Item.Index;
            for (int j = 0; j < listItemsAss.Items.Count; j++)
            {
                if (listItemsAss.Items[j].Checked == true)
                    listItemsAss.Items[j].Checked = false;
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    for (int j = 0; j < listItemsAss.Items.Count; j++)
                    {
                        if (ds.Tables[0].Rows[i]["ItemID"].ToString() == listItemsAss.Items[j].SubItems[3].Text)
                        {
                            listItemsAss.Items[j].Checked = true;
                            //break;
                        }
                    }
                }
            }
        }
    }
        private void listGroups_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (CheckedGroupID != -1)
            {
                listGroups.Items[CheckedGroupID].Checked = false;
                //listGroupsAss.Items[CheckedGroupID].Checked = false;
            }
            if (e.Item.Checked == true)
            {

                if (e.Item.Text != "All Groups")
                {
                    CheckedGroupID = Convert.ToInt16(e.Item.Index.ToString());
                    //listGroupsAss.Items[CheckedGroupID].Checked = true;
                    GetItemsByGroup(Convert.ToInt16(e.Item.SubItems[1].Text));
                }
                else
                {
                    CheckedGroupID = 0;
                    //listGroupsAss.Items[CheckedGroupID].Checked = true;
                    GetItemsByGroup(0);
                }
            }
            //for (int i = 0; i < listGroups.Items.Count; i++)
            //{ 
            //    if(listGroups.Items[i].Checked==true)
            //    {
            //        GetItemsByGroup(Convert.ToInt16(listGroups.Items[i].SubItems[0].Text));
            //    }
            //}
        }

        private void listItems_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (CheckedItemID != -1)
                listItems.Items[CheckedItemID].Checked = false;
            if (e.Item.Checked == true)
            {
                CheckedItemID = Convert.ToInt16(e.Item.Index.ToString());
                txtItemName.Enabled = true;
                ddlGroup.Enabled = true;
                ddlMaterialType.Enabled = true;
                txtCost.Enabled = true;
                ddlCostPer.Enabled = true;
                txtCoverage.Enabled = true;
                txtWaste.Enabled = true;
                txtLabour.Enabled = true;
                groupBox1.Enabled = true;
                btnSaveItem.Enabled = true;
                GetItemsByID(Convert.ToInt16(e.Item.SubItems[3].Text));
            }
        }

        private void btnAddGroup_Click(object sender, EventArgs e)
        {
            txtAddGroup.Visible = true;
            btnSaveGroup.Visible = true;
            btnSaveGroup.Text = "Save";
            btnCancel.Visible = true;
            btnEditGroup.Enabled = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtAddGroup.Visible = false;
            btnSaveGroup.Visible = false;
            btnCancel.Visible = false;
            btnEditGroup.Enabled = true;
            btnAddGroup.Enabled = true;
        }

        private void btnSaveGroup_Click(object sender, EventArgs e)
        {
            if (txtAddGroup.Text == "")
            {
                MessageBox.Show("Please Enter a Group Name or click Cancel");
                return;
            }
            else
            {
                if (btnSaveGroup.Text != "Update")
                {
                    objEntities.Action = 1;
                    objEntities.GroupID = 0;
                    objEntities.GroupName = txtAddGroup.Text;
                }
                else
                {
                    objEntities.Action = 2;
                    objEntities.GroupID = GroupID1;
                    objEntities.GroupName = txtAddGroup.Text;
                }
                DataSet ds = new DataSet();
                ds=objManageItems.GetGroups(objEntities);
                txtAddGroup.Visible = false; ;
                txtAddGroup.Text = "";
                btnSaveGroup.Visible = false;
                btnCancel.Visible = false;
                GetGroups();

            }
            btnAddGroup.Enabled = true;
            btnEditGroup.Enabled = true;
        }

        private void btnEditGroup_Click(object sender, EventArgs e)
        {
            
            btnSaveGroup.Text = "Update";
            string chk = "";
            for (int j = 0; j < listGroups.Items.Count; j++)
            {
                if (j != 0)
                {
                    if (listGroups.Items[j].Checked == true)
                    {
                        chk = "1";
                    }
                }
            }
            if (chk == "")
            {
                MessageBox.Show("Please select a group to edit");
                return;
            }
            else
            {
                btnAddGroup.Enabled = false;
                txtAddGroup.Visible = true;
                btnSaveGroup.Visible = true;
                btnCancel.Visible = true;
                for (int i = 0; i < listGroups.Items.Count; i++)
                {
                    if (listGroups.Items[i].Checked == true)
                    {
                        txtAddGroup.Text = listGroups.Items[i].Text;
                        GroupID1 = Convert.ToInt16(listGroups.Items[i].SubItems[1].Text);
                    }
                }
            }
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            txtItemName.Focus();
            txtItemName.Enabled = true;
            ddlGroup.Enabled = true;
            ddlMaterialType.Enabled = true;
            txtCost.Enabled = true;
            ddlCostPer.Enabled = true;
            txtCoverage.Enabled = true;
            txtWaste.Enabled = true;
            txtLabour.Enabled = true;
            groupBox1.Enabled = true;
            btnSaveItem.Enabled = true;
            GetItemsByID(0);
        }

        private void btnSaveItem_Click(object sender, EventArgs e)
        {
            if (ItemID1 == 0)
            {
                objEntities.Action = 0;
                objEntities.ItemID = 0;
            }
            else
            {
                objEntities.Action = 1;
                objEntities.ItemID = ItemID1;
            }
            objEntities.ItemName= txtItemName.Text;
            objEntities.GroupID=Convert.ToInt16( ddlGroup.SelectedValue);
            objEntities.MaterialTypeID=Convert.ToInt16( ddlMaterialType.SelectedValue);
            if (txtCost.Text != "")
                objEntities.Cost = Convert.ToDecimal(txtCost.Text);
            else
                objEntities.Cost = 0;
            if (ddlCostPer.SelectedIndex != -1)
                objEntities.CostPer = ddlCostPer.SelectedItem.ToString();
            else
                objEntities.CostPer = "";
            objEntities.Coverage= txtCoverage.Text;
            objEntities.Waste = txtWaste.Text;
            objEntities.Labour= txtLabour.Text;
            if (radioButton1.Checked == true)
                objEntities.LabourType = 1;
            else
                objEntities.LabourType = 2;

            objManageItems.InsertItems(objEntities);
            //MessageBox.Show("Item Saved Successfully");
            GetItems();
            GetItemsByID(Convert.ToInt16(objManageItems.GetMaxItemID().Tables[0].Rows[0][0].ToString()));
            listItems.Refresh();
        }

        private void listGroups_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listGroupsAss_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listGroupsAss_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (CheckedGroupIDAss != -1)
            {
                listGroupsAss.Items[CheckedGroupIDAss].Checked = false;
                //listGroups.Items[CheckedGroupID].Checked = false;
            }
            if (e.Item.Checked == true)
            {

                if (e.Item.Text != "All Groups")
                {
                    CheckedGroupIDAss = Convert.ToInt16(e.Item.Index);
                    //listGroups.Items[CheckedGroupID].Checked = true;
                    GetItemsByGroup(Convert.ToInt16(e.Item.SubItems[1].Text));
                }
                else
                {
                    CheckedGroupIDAss = 0;
                    //listGroups.Items[CheckedGroupID].Checked = true;
                    GetItemsByGroup(0);
                }
            }
        }
        private void GetProjectDetails(DataSet ds)
        {
            listCurrentProjects.Items.Clear();
            listOldProjects.Items.Clear();
            int MaxProjectID = 0;
            int ProjectID = 0;
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ProjectID = Convert.ToInt16(ds.Tables[0].Rows[i]["ProjectID"].ToString());
                    if (ds.Tables[0].Rows[i]["Status"].ToString() == "False")
                    {
                        if (ProjectID > MaxProjectID)
                        {
                            MaxProjectID = ProjectID;
                        }
                        objProjectEntities.ProjectID = MaxProjectID;
                        ProjectDB.ProjectID = MaxProjectID;
                        ListViewItem lv = new ListViewItem(ds.Tables[0].Rows[i]["ProjectName"].ToString());
                        lv.SubItems.Add(ds.Tables[0].Rows[i]["ProjectID"].ToString());
                        listCurrentProjects.Items.Add(lv);
                    }
                    else
                    {
                        ListViewItem lv = new ListViewItem(ds.Tables[0].Rows[i]["ProjectName"].ToString());
                        lv.SubItems.Add(ds.Tables[0].Rows[i]["ProjectID"].ToString());
                        listOldProjects.Items.Add(lv);
                    }
                }
            }
            else
            {
                txtProjectID.Text = "";
                txtProjectName.Text = "";
                txtProjectAddress.Text = "";
                txtProjectCity.Text = "";
                txtProjectState.Text = "";
                txtProjectZip.Text = "";
                txtProjectPhone.Text = "";
                txtProjectFax.Text = "";
            }
            ds.Dispose();
        }

        private void BindAssemblies()
        {
            DataSet ds = new DataSet();
            objEntities.AssemblyName = "";
            objEntities.AssemblyID = 0;
            objEntities.Action = 0;
            ds = objManageItems.GetAssemblies(objEntities);
            if (ds.Tables[0].Rows.Count > 0)
            {
                ddlAssemblies.DataSource = ds.Tables[0];
                ddlAssemblies.DisplayMember = "AssemblyName";
                ddlAssemblies.ValueMember = "AssemblyID";
            }
        }

        public void tab1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tab1.SelectedIndex == 0)
            {
                tab1.TabPages[0].ImageIndex = 0;
                tab1.TabPages[1].ImageIndex = 2;
                tab1.TabPages[2].ImageIndex = 4;
                tab1.TabPages[3].ImageIndex = 6;
                tab1.TabPages[4].ImageIndex = 8;
                tab1.TabPages[5].ImageIndex = 10;
                tab1.TabPages[6].ImageIndex = 12;
                tab1.TabPages[7].ImageIndex = 14;
                tab1.TabPages[8].ImageIndex = 16;
            }
            if (tab1.SelectedIndex == 1)
            {
                tab1.TabPages[0].ImageIndex = 1;
                tab1.TabPages[1].ImageIndex = 3;
                tab1.TabPages[2].ImageIndex = 4;
                tab1.TabPages[3].ImageIndex = 6;
                tab1.TabPages[4].ImageIndex = 8;
                tab1.TabPages[5].ImageIndex = 10;
                tab1.TabPages[6].ImageIndex = 12;
                tab1.TabPages[7].ImageIndex = 14;
                tab1.TabPages[8].ImageIndex = 16;

                BindAssemblies();

                
            }
            if (tab1.SelectedIndex == 2)
            {
                tab1.TabPages[0].ImageIndex = 1;
                tab1.TabPages[1].ImageIndex = 2;
                tab1.TabPages[2].ImageIndex = 5;
                tab1.TabPages[3].ImageIndex = 6;
                tab1.TabPages[4].ImageIndex = 8;
                tab1.TabPages[5].ImageIndex = 10;
                tab1.TabPages[6].ImageIndex = 12;
                tab1.TabPages[7].ImageIndex = 14;
                tab1.TabPages[8].ImageIndex = 16;
                DataSet ds = new DataSet();
                objProjectEntities.ProjectID = 0;
                ds = objProjectDB.GetProjectDetails(objProjectEntities);
                GetProjectDetails(ds);
            }
            if (tab1.SelectedIndex == 3)
            {
                tab1.TabPages[0].ImageIndex = 1;
                tab1.TabPages[1].ImageIndex = 2;
                tab1.TabPages[2].ImageIndex = 4;
                tab1.TabPages[3].ImageIndex = 7;
                tab1.TabPages[4].ImageIndex = 8;
                tab1.TabPages[5].ImageIndex = 10;
                tab1.TabPages[6].ImageIndex = 12;
                tab1.TabPages[7].ImageIndex = 14;
                tab1.TabPages[8].ImageIndex = 16;
            }
            if (tab1.SelectedIndex == 4)
            {
                tab1.TabPages[0].ImageIndex = 1;
                tab1.TabPages[1].ImageIndex = 2;
                tab1.TabPages[2].ImageIndex = 4;
                tab1.TabPages[3].ImageIndex = 6;
                tab1.TabPages[4].ImageIndex = 9;
                tab1.TabPages[5].ImageIndex = 10;
                tab1.TabPages[6].ImageIndex = 12;
                tab1.TabPages[7].ImageIndex = 14;
                tab1.TabPages[8].ImageIndex = 16;

                loadMap();

            }
            if (tab1.SelectedIndex == 5)
            {
                tab1.TabPages[0].ImageIndex = 1;
                tab1.TabPages[1].ImageIndex = 2;
                tab1.TabPages[2].ImageIndex = 4;
                tab1.TabPages[3].ImageIndex = 6;
                tab1.TabPages[4].ImageIndex = 8;
                tab1.TabPages[5].ImageIndex = 11;
                tab1.TabPages[6].ImageIndex = 12;
                tab1.TabPages[7].ImageIndex = 14;
                tab1.TabPages[8].ImageIndex = 16;

                BindMeasurementstoGrid();
                //DataSet ds = new DataSet();
                //ds = objProjectDB.GetMeasurements(objProjectEntities);
                //  smK_EditListView1.Items.Clear();
                //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                //{
                //    gridMeasurements.DataSource = ds.Tables[0];
                //    gridMeasurements.Columns[0].Width = 250;
                //    gridMeasurements.Columns[1].Width = 120;
                //    gridMeasurements.Columns[2].Width = 150;
                //    gridMeasurements.Columns[3].Visible = false;
                    
                //    //gridMeasurements.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i]["Description"].ToString();
                //    //gridMeasurements.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i]["Measurement"].ToString();
                //    //gridMeasurements.Rows[i].Cells[2].Value = ds.Tables[0].Rows[i]["MeasurementUnits"].ToString();
                //    //gridMeasurements.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i]["MeasurementID"].ToString();

                //    ListViewItem lstItem = new ListViewItem(ds.Tables[0].Rows[i]["Description"].ToString() );
                //    lstItem.SubItems.Add(ds.Tables[0].Rows[i]["Measurement"].ToString());
                //    lstItem.SubItems.Add(ds.Tables[0].Rows[i]["MeasurementUnits"].ToString());
                //    lstItem.SubItems.Add(ds.Tables[0].Rows[i]["MeasurementID"].ToString());
                //    smK_EditListView1.Items.Add(lstItem);
                    
                //}
                DataBinding = 1;
            }
            if (tab1.SelectedIndex == 6)
            {
                tab1.TabPages[0].ImageIndex = 1;
                tab1.TabPages[1].ImageIndex = 2;
                tab1.TabPages[2].ImageIndex = 4;
                tab1.TabPages[3].ImageIndex = 6;
                tab1.TabPages[4].ImageIndex = 8;
                tab1.TabPages[5].ImageIndex = 10;
                tab1.TabPages[6].ImageIndex = 13;
                tab1.TabPages[7].ImageIndex = 14;
                tab1.TabPages[8].ImageIndex = 16;

                BindDatatoWorkSheet();
                //for (int i = 0; i < dsMeasurements.Tables[0].Rows.Count; i++)
                //{

                //    ListViewItem lstItem = new ListViewItem(dsMeasurements.Tables[0].Rows[i]["Description"].ToString());
                //    lstItem.SubItems.Add(dsMeasurements.Tables[0].Rows[i]["Measurement"].ToString());
                //    lstItem.SubItems.Add(dsMeasurements.Tables[0].Rows[i]["MeasurementUnits"].ToString());
                //    smK_EditListView1.Items.Add(lstItem);

                //    editListView1.Items.Add(dsMeasurements.Tables[0].Rows[i]["Description"].ToString());
                //    editListView1.Items[i].SubItems[1].Text = dsMeasurements.Tables[0].Rows[i]["Measurement"].ToString();
                //    editListView1.Items[i].SubItems[2].Text = dsMeasurements.Tables[0].Rows[i]["Measurement"].ToString();
                //    editListView1.Items[i].SubItems[3].Text = "";
                //    editListView1.Items[i].SubItems[4].Text = dsMeasurements.Tables[0].Rows[i]["MeasurementUnits"].ToString();
                    
                //    //if (ds.Tables[0].Rows[i]["HavingChild"].ToString() == "1")
                //    //{
                //    //    editListView1.Items.Add(ds.Tables[0].Rows[i]["Description"].ToString());
                //    //    editListView1.Items[i+1].SubItems[1].Text = ds.Tables[0].Rows[i]["Measurement"].ToString();
                //    //    editListView1.Items[i + 1].SubItems[2].Text = ds.Tables[0].Rows[i]["Measurement"].ToString();
                //    //    editListView1.Items[i + 1].SubItems[3].Text = "";
                //    //    editListView1.Items[i + 1].SubItems[4].Text = ds.Tables[0].Rows[i]["MeasurementUnits"].ToString();
                //    //}
                //    //editListView1.Items[i].SubItems[5].Text = ""; 


                //    //ListViewItem lstItem1 = new ListViewItem(ds.Tables[0].Rows[i]["Description"].ToString());
                //    //lstItem1.SubItems.Add(ds.Tables[0].Rows[i]["Measurement"].ToString());
                //    //lstItem1.SubItems.Add(ds.Tables[0].Rows[i]["Measurement"].ToString());
                //    //lstItem1.SubItems.Add("");
                //    //lstItem1.SubItems.Add(ds.Tables[0].Rows[i]["MeasurementUnits"].ToString());
                //    //editListView1.Items.Add(lstItem1);  

                //}

            }
            if (tab1.SelectedIndex == 7)
            {
                tab1.TabPages[0].ImageIndex = 1;
                tab1.TabPages[1].ImageIndex = 2;
                tab1.TabPages[2].ImageIndex = 4;
                tab1.TabPages[3].ImageIndex = 6;
                tab1.TabPages[4].ImageIndex = 8;
                tab1.TabPages[5].ImageIndex = 10;
                tab1.TabPages[6].ImageIndex = 12;
                tab1.TabPages[7].ImageIndex = 15;
                tab1.TabPages[8].ImageIndex = 16;

                decimal Material = 0;
                decimal Labour = 0;
                decimal Equipment = 0;
                for (int i = 0; i < gridMeasurementChild.Rows.Count; i++)
                {
                    if (gridMeasurementChild.Rows[i].Cells[11].Value.ToString() != "")
                    {
                        Material = Material + Convert.ToDecimal(gridMeasurementChild.Rows[i].Cells[11].Value.ToString());
                    }
                    Material = decimal.Round(Material, 3);
                    if (gridMeasurementChild.Rows[i].Cells[12].Value.ToString() != "")
                    {
                        string labor = gridMeasurementChild.Rows[i].Cells[12].Value.ToString();
                        labor = labor.Replace("$", "");
                        Labour = Labour + Convert.ToDecimal(labor);
                    }
                    Labour = decimal.Round(Labour, 3);
                    if (gridMeasurementChild.Rows[i].Cells[12].Value.ToString() != "")
                    {
                        string equp = gridMeasurementChild.Rows[i].Cells[13].Value.ToString();
                        equp = equp.Replace("$", "");
                        Equipment = Equipment + Convert.ToDecimal(equp);
                    }
                    Equipment = decimal.Round(Equipment, 3);
                }
                resTotalMaterial.Text = Material.ToString();
                lblMaterialSubtotal.Text = resTotalMaterial.Text;
                if (txtMaterialPercentage.Text != "")
                {
                    decimal percentage = Convert.ToDecimal(txtMaterialPercentage.Text) / 100;
                    decimal percentagetotal = Convert.ToDecimal(resTotalMaterial.Text) * percentage;
                    percentagetotal = decimal.Round(percentagetotal, 3);
                    lblMaterialSubtotal.Text = Convert.ToString(decimal.Round(Convert.ToDecimal((resTotalMaterial.Text) + percentagetotal),3));

                }
                if (txtLabourPercentage.Text != "")
                {
                    decimal percentage = Convert.ToDecimal(txtLabourPercentage.Text) / 100;
                    decimal percentagetotal = Convert.ToDecimal(resTotalLabor.Text) * percentage;
                    lblLaborSubtotal.Text = Convert.ToString(decimal.Round(Convert.ToDecimal((resTotalLabor.Text) + percentagetotal),3));
                }

                decimal grandsubtotal = Convert.ToDecimal(lblMaterialSubtotal.Text) + Convert.ToDecimal(lblLaborSubtotal.Text);
                grandsubtotal = decimal.Round(grandsubtotal, 3);
                lblGrandSubtotal.Text = grandsubtotal.ToString();
                resTotalLabor.Text = Labour.ToString();
                resTotalEquipment.Text = Equipment.ToString();

                decimal total=Material+Labour+Equipment;
                total = decimal.Round(total, 3);
                lblResGrandTotal.Text = total.ToString();
                lblGrandSubtotal.Text = lblResGrandTotal.Text;
                BindMiscItemstoGrid();

                lblFinalSubTotal.Text =Convert.ToString(decimal.Round((Convert.ToDecimal(lblGrandSubtotal.Text) + MiscItemCostTotal),3));
                
            }
            if (tab1.SelectedIndex == 8)
            {
                tab1.TabPages[0].ImageIndex = 1;
                tab1.TabPages[1].ImageIndex = 2;
                tab1.TabPages[2].ImageIndex = 4;
                tab1.TabPages[3].ImageIndex = 6;
                tab1.TabPages[4].ImageIndex = 8;
                tab1.TabPages[5].ImageIndex = 10;
                tab1.TabPages[6].ImageIndex = 12;
                tab1.TabPages[7].ImageIndex = 14;
                tab1.TabPages[8].ImageIndex = 17;

                DataSet ds = new DataSet();
                DataSet dsMeasure = new DataSet();
                objProjectEntities.ProjectID = ProjectDB.ProjectID;
                dsMeasure = objProjectDB.LoadMeasurements(objProjectEntities);
                
                ds = objProjectDB.GetMeasurementChilds(objProjectEntities);

                foreach (DataRow dr in dsMeasure.Tables[0].Rows)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        if (dr["MeasurementID"].ToString() == ds.Tables[0].Rows[i]["MeasurementID"].ToString())
                        {
                            TreeNode node;
                            node= new TreeNode();
                            node.Text = ds.Tables[0].Rows[i]["Description"].ToString();
                            treeView1.Nodes.Add(node);

                            node = new TreeNode();
                            node.Text = ds.Tables[0].Rows[i]["ItemName"].ToString();
                            treeView1.Nodes[0].Nodes.Add(node);
                            
                            string description = ds.Tables[0].Rows[i]["Description"].ToString();
                            string[] des = new string[2];
                            des = description.Split(' ');
                            
                            treeView1.Nodes.Add(des[1].ToString());
                            treeView1.Nodes[0].Text = des[4].ToString();
                            treeView1.Nodes[0].Nodes.Add(des[4].ToString());
                            treeView1.Nodes[1].Text = des[4].ToString();
                            
                            treeView1.Nodes[0].Nodes[0].Text = des[4].ToString();
                            //treeView1.Nodes[0].Nodes.Add(ds.Tables[0].Rows[i]["ItemName"].ToString());
                            //treeView1.Nodes[0].Nodes[0].Nodes.Add(ds.Tables[0].Rows[i]["Description"].ToString());
                            treeView1.Nodes[0].Nodes[0].Text = ds.Tables[0].Rows[i]["Description"].ToString();
                            //treeView1.Nodes[0].Nodes[1].Nodes.Add("Total "+ds.Tables[0].Rows[i]["Cost"].ToString());
                            treeView1.Nodes[0].Nodes[0].Nodes[1].Text = "Total " + ds.Tables[0].Rows[i]["Cost"].ToString();
                        }
                    }
                }
                
            }
        }

        private void BindMeasurementstoGrid()
        {
            DataSet ds = new DataSet();
            ds = objProjectDB.GetMeasurements(objProjectEntities);
            gridMeasurements.Columns.Clear();
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    gridMeasurements.DataSource = ds.Tables[0];
                    gridMeasurements.Columns[0].Width = 250;
                    gridMeasurements.Columns[1].Width = 120;
                    gridMeasurements.Columns[2].Width = 130;
                    gridMeasurements.Columns[3].Visible = false;

                    //gridMeasurements.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i]["Description"].ToString();
                    //gridMeasurements.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i]["Measurement"].ToString();
                    //gridMeasurements.Rows[i].Cells[2].Value = ds.Tables[0].Rows[i]["MeasurementUnits"].ToString();
                    //gridMeasurements.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i]["MeasurementID"].ToString();

                    ListViewItem lstItem = new ListViewItem(ds.Tables[0].Rows[i]["Description"].ToString());
                    lstItem.SubItems.Add(ds.Tables[0].Rows[i]["Measurement"].ToString());
                    lstItem.SubItems.Add(ds.Tables[0].Rows[i]["MeasurementUnits"].ToString());
                    lstItem.SubItems.Add(ds.Tables[0].Rows[i]["MeasurementID"].ToString());
                    smK_EditListView1.Items.Add(lstItem);

                }
            }
            else
            {
                DataGridViewTextBoxColumn Description = new DataGridViewTextBoxColumn();
                Description.Width = 250;
                Description.HeaderText = "Description";
                gridMeasurements.Columns.Add(Description);
                
                DataGridViewTextBoxColumn Measurement = new DataGridViewTextBoxColumn();
                Measurement.Width = 120;
                Measurement.HeaderText = "Measurement";
                gridMeasurements.Columns.Add(Measurement);
                
                DataGridViewTextBoxColumn MeasurementUnits = new DataGridViewTextBoxColumn();
                MeasurementUnits.HeaderText = "MeasurementUnits";
                MeasurementUnits.Width = 110;
                gridMeasurements.Columns.Add(MeasurementUnits);
                
                DataGridViewTextBoxColumn MeasurementID = new DataGridViewTextBoxColumn();
                MeasurementID.Width = 0;
                gridMeasurements.Columns.Add(MeasurementID);
            }
        }

        private void BindMiscItemstoGrid()
        {
            DataSet ds = new DataSet();
            ds = objProjectDB.GetMiscItems(objProjectEntities);
            dataGridView3.DataSource = ds.Tables[0];

            dataGridView3.Columns[0].Width = 570;
            dataGridView3.Columns[1].Width = 130;

            //dataGridView3.Columns[0].Visible = false;
            dataGridView3.Columns[2].Visible = false;
            dataGridView3.Columns[3].Visible = false;

            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (ds.Tables[0].Rows[i]["MiscItemCost"].ToString()!="")
                        MiscItemCostTotal =MiscItemCostTotal+ Convert.ToDecimal(ds.Tables[0].Rows[i]["MiscItemCost"].ToString());
                }
            }
        }

        public void BindDatatoWorkSheet()
        {
            DataSet dsMeasurements = new DataSet();
            objProjectEntities.ProjectID = ProjectDB.ProjectID;
            dsMeasurements = objProjectDB.LoadMeasurements(objProjectEntities);
            DataSet dsMeasurementChild = new DataSet();
            dsMeasurementChild = objProjectDB.GetMeasurementChilds(objProjectEntities);

            foreach (DataRow drRow in dsMeasurementChild.Tables[0].Rows)
            {
                decimal waste = 0;
                decimal avg = 0;
                decimal PurchaseUnits = 0;
                decimal Material = 0;
                decimal Labour = 0;
                decimal CorrectedMeasurement = 0;
                decimal Layers=0;
                decimal Coverge=0;
                decimal CostPurchasePerUnit=0;
                string Note = drRow["Note"].ToString();
                if (Note.Length>9)
                drRow["Note"] = Note.Substring(0, 10);
                if (drRow["CorrectedMeasurement"].ToString() != "")
                    CorrectedMeasurement =decimal.Round(Convert.ToDecimal(drRow["CorrectedMeasurement"].ToString()),3);
                else
                    CorrectedMeasurement = 0;
                if (drRow["Layers"].ToString() != "")
                    Layers =decimal.Round(Convert.ToDecimal(drRow["Layers"].ToString()),3);
                else
                    Layers = 0;
                if (drRow["Coverage"].ToString() != "")
                    Coverge =decimal.Round(Convert.ToDecimal(drRow["Coverage"].ToString()),3);
                else
                    Coverge = 1;
                if (drRow["CostPerUnit"].ToString() != "")
                    CostPurchasePerUnit =decimal.Round(Convert.ToDecimal(drRow["CostPerUnit"].ToString()),3);
                else
                    CostPurchasePerUnit = 0;

                decimal res = (CorrectedMeasurement * Layers) / Coverge;

                if (drRow["Waste"].ToString() != "")
                {
                    waste = Convert.ToDecimal(drRow["Waste"].ToString());
                    waste = waste / 100;

                    avg = res * waste;
                }
                PurchaseUnits = res + avg;
                PurchaseUnits = decimal.Round(PurchaseUnits, 3);
                drRow["Units"] = PurchaseUnits.ToString();
                
                Material = PurchaseUnits * CostPurchasePerUnit;
                Material = decimal.Round(Material, 3);
                drRow["Material"] = Material.ToString();

                if (drRow["Labour"].ToString() != "")
                    Labour = CorrectedMeasurement * Convert.ToDecimal(drRow["Labour"].ToString());

                drRow["Labor"] = "$" + Labour.ToString();
                drRow["Equipment"] = "$0.00";
            }

            DataSet dsFinal = new DataSet();
            dsFinal = dsMeasurements.Copy();

            dsFinal.Clear();

            for (int i = 0; i < dsMeasurements.Tables[0].Rows.Count; i++)
            {
                DataRow dr;
                dr = dsMeasurements.Tables[0].Rows[i];
                string Note = dr["Note"].ToString();
                if (Note.Length > 5)
                {
                    Note = Note.Substring(0, 5);
                    dr["Note"] = Note;
                }
                dsFinal.Tables[0].ImportRow(dr);
                if (dsMeasurements.Tables[0].Rows[i]["HavingChild"].ToString() == "1")
                {
                    DataRow dr1;
                    for (int j = 0; j < dsMeasurementChild.Tables[0].Rows.Count; j++)
                    {
                        if (dsMeasurementChild.Tables[0].Rows[j]["MeasurementID"].ToString() == dsMeasurements.Tables[0].Rows[i]["MeasurementID"].ToString())
                        {
                            dr1 = dsMeasurementChild.Tables[0].Rows[j];
                            
                            dsFinal.Tables[0].ImportRow(dr1);
                        }
                    }
                }
            }

            gridMeasurementChild.DataSource = null;
            gridMeasurementChild.DataSource = dsFinal.Tables[0];
           
            dsFinal.Dispose();
            for (int i = 0; i < 24; i++)
            {
                gridMeasurementChild.Columns[i].SortMode = 0;
                
            }
            for (int j = 0; j < dsFinal.Tables[0].Rows.Count; j++)
            {
                if (gridMeasurementChild.Rows[j].Cells["ID"].Value.ToString() == "0")
                {
                    DataGridViewCellStyle style = new DataGridViewCellStyle();
                    style.BackColor = Color.LightBlue;
                    gridMeasurementChild.Rows[j].Cells["Description"].Style = style;
                }
                else 
                {
                    DataGridViewCellStyle style = new DataGridViewCellStyle();
                    style.BackColor = Color.Orange;
                    gridMeasurementChild.Rows[j].Cells["Description"].Style = style;
                }
            }
            gridMeasurementChild.AutoResizeColumn(0);
            gridMeasurementChild.AutoResizeColumn(1);
            gridMeasurementChild.AutoResizeColumn(2);
            gridMeasurementChild.AutoResizeColumn(3);
            gridMeasurementChild.AutoResizeColumn(4);
            gridMeasurementChild.AutoResizeColumn(5);
            gridMeasurementChild.AutoResizeColumn(6);
            gridMeasurementChild.AutoResizeColumn(7);
            gridMeasurementChild.AutoResizeColumn(8);
            gridMeasurementChild.AutoResizeColumn(9);
            gridMeasurementChild.AutoResizeColumn(10);
            gridMeasurementChild.AutoResizeColumn(11);
            gridMeasurementChild.AutoResizeColumn(12);
            gridMeasurementChild.AutoResizeColumn(13);
            gridMeasurementChild.Columns[14].Visible = false;
            gridMeasurementChild.Columns[15].Visible = false;
            gridMeasurementChild.Columns[16].Visible = false;
            gridMeasurementChild.Columns[17].Visible = false;
            gridMeasurementChild.Columns[18].Visible = false;
            gridMeasurementChild.Columns[19].Visible = false;
            gridMeasurementChild.Columns[20].Visible = false;
            gridMeasurementChild.Columns[21].Visible = false;
            gridMeasurementChild.Columns[22].Visible = false;
            gridMeasurementChild.Columns[23].Visible = false;
            gridMeasurementChild.Columns[24].Visible = false;

            gridMeasurementChild.Columns[0].Width = 250;
            smK_EditListView1.Items.Clear();
            editListView1.Items.Clear();
        }

        private void btnMovetoOld_Click(object sender, EventArgs e)
        {
            objProjectEntities.Action = 0;
            for (int i = 0; i < listCurrentProjects.Items.Count; i++)
            {
                if (listCurrentProjects.Items[i].Selected == true)
                    objProjectEntities.ProjectID = Convert.ToInt16(listCurrentProjects.Items[i].SubItems[1].Text);
            }
            objProjectDB.ChangeProjectStatus(objProjectEntities);
            DataSet ds = new DataSet();
            objProjectEntities.ProjectID = 0;
            ds = objProjectDB.GetProjectDetails(objProjectEntities);
            GetProjectDetails(ds);
        }

        private void listCurrentProjects_DoubleClick(object sender, EventArgs e)
        {
            tab1.SelectedIndex = 3;
            for (int i = 0; i < listCurrentProjects.Items.Count; i++)
            {
                if (listCurrentProjects.Items[i].Selected == true)
                {
                    objProjectEntities.ProjectID = Convert.ToInt16(listCurrentProjects.Items[i].SubItems[1].Text);
                    ProjectDB.ProjectID = Convert.ToInt16(listCurrentProjects.Items[i].SubItems[1].Text);
                }
            }
            DataSet ds = new DataSet();
            ds = objProjectDB.GetProjectDetails(objProjectEntities);
            if (ds.Tables[0].Rows.Count > 0)
            {
                txtProjectID.Text = ds.Tables[0].Rows[0]["ProjectID"].ToString();
                txtProjectName.Text = ds.Tables[0].Rows[0]["ProjectName"].ToString();
                txtProjectAddress.Text = ds.Tables[0].Rows[0]["Address"].ToString();
                txtProjectCity.Text = ds.Tables[0].Rows[0]["City"].ToString();
                txtProjectState.Text = ds.Tables[0].Rows[0]["State"].ToString();
                txtProjectZip.Text = ds.Tables[0].Rows[0]["Zip"].ToString();
                txtProjectPhone.Text = ds.Tables[0].Rows[0]["Phone"].ToString();
                txtProjectFax.Text = ds.Tables[0].Rows[0]["Fax"].ToString();
                txtDny1.Text = ds.Tables[0].Rows[0]["txtDny1"].ToString();
                txtDny2.Text = ds.Tables[0].Rows[0]["txtDny2"].ToString();
                txtDny3.Text = ds.Tables[0].Rows[0]["txtDny3"].ToString();
                txtDny4.Text = ds.Tables[0].Rows[0]["txtDny4"].ToString();
                txtDny5.Text = ds.Tables[0].Rows[0]["txtDny5"].ToString();
                DnyLbl1.Text = ds.Tables[0].Rows[0]["DnyLbl1"].ToString();
                DnyLbl2.Text = ds.Tables[0].Rows[0]["DnyLbl2"].ToString();
                DnyLbl3.Text = ds.Tables[0].Rows[0]["DnyLbl3"].ToString();
                DnyLbl4.Text = ds.Tables[0].Rows[0]["DnyLbl4"].ToString();
                DnyLbl5.Text = ds.Tables[0].Rows[0]["DnyLbl5"].ToString();
            }
        }

        private void btnMovetoCurrent_Click(object sender, EventArgs e)
        {
            objProjectEntities.Action = 1;
            for (int i = 0; i < listOldProjects.Items.Count; i++)
            {
                if (listOldProjects.Items[i].Selected == true)
                    objProjectEntities.ProjectID = Convert.ToInt16(listOldProjects.Items[i].SubItems[1].Text);
            }
            objProjectDB.ChangeProjectStatus(objProjectEntities);
            DataSet ds = new DataSet();
            objProjectEntities.ProjectID = 0;
            ds = objProjectDB.GetProjectDetails(objProjectEntities);
            GetProjectDetails(ds);
        }

        private void btnSaveProjectDetails_Click(object sender, EventArgs e)
        {
            if (txtProjectID.Text != "")
                objProjectEntities.ProjectID = Convert.ToInt16(txtProjectID.Text);
            else
                objProjectEntities.ProjectID = 0;
            objProjectEntities.ProjectName = txtProjectName.Text;
            objProjectEntities.Address = txtProjectAddress.Text;
            objProjectEntities.City = txtProjectCity.Text;
            objProjectEntities.State = txtProjectState.Text;
            objProjectEntities.Zip = txtProjectZip.Text;
            objProjectEntities.Phone = txtProjectPhone.Text;
            objProjectEntities.Fax = txtProjectFax.Text;
            objProjectEntities.Notes = txtNotes.Text;
            if (txtLbl1.Visible != false)
            {
                if (txtLbl1.Text != "")
                    DnyLbl1.Text = txtLbl1.Text;
                if (txtLbl2.Text != "")
                    DnyLbl2.Text = txtLbl2.Text;
                if (txtLbl3.Text != "")
                    DnyLbl3.Text = txtLbl3.Text;
                if (txtLbl4.Text != "")
                    DnyLbl4.Text = txtLbl4.Text;
                if (txtLbl5.Text != "")
                    DnyLbl5.Text = txtLbl5.Text;
            }
            objProjectEntities.DnyLbl1 = DnyLbl1.Text;
            objProjectEntities.DnyLbl2 = DnyLbl2.Text;
            objProjectEntities.DnyLbl3 = DnyLbl3.Text;
            objProjectEntities.DnyLbl4 = DnyLbl4.Text;
            objProjectEntities.DnyLbl5 = DnyLbl5.Text;
            objProjectEntities.TxtDny1 = txtDny1.Text;
            objProjectEntities.TxtDny2 = txtDny2.Text;
            objProjectEntities.TxtDny3 = txtDny3.Text;
            objProjectEntities.TxtDny4 = txtDny4.Text;
            objProjectEntities.TxtDny5 = txtDny5.Text;

            objProjectDB.ManageProjectDetails(objProjectEntities);

            MessageBox.Show("Project Saved Successfully");
        }

        private void btnAddNewProject_Click(object sender, EventArgs e)
        {
            tab1.SelectedIndex = 3;
            objProjectEntities.ProjectID = -1;
            DataSet ds = new DataSet();
            ds = objProjectDB.GetProjectDetails(objProjectEntities);
            GetProjectDetails(ds);
        }

        void loadMap()
        {
            try
            {
                string street = string.Empty;
                string city = string.Empty;
                string state = string.Empty;
                string zip = string.Empty;

                StringBuilder queryAddress = new StringBuilder();
                queryAddress.Append("http://maps.google.com/maps?q=");

                // build street part of query string 
                if (txtProjectAddress.Text != string.Empty)
                {
                    street = txtProjectAddress.Text.Replace(" ", "+");
                    queryAddress.Append(street + "," + "+");
                }

                // build city part of query string 
                if (txtProjectCity.Text != string.Empty)
                {
                    city = txtProjectCity.Text.Replace(" ", "+");
                    queryAddress.Append(city + "," + "+");
                }

                // build state part of query string 
                if (txtProjectState.Text != string.Empty)
                {
                    state = txtProjectState.Text.Replace(" ", "+");
                    queryAddress.Append(state + "," + "+");
                }

                // build zip code part of query string 
                if (txtProjectZip.Text != string.Empty)
                {
                    zip = txtProjectZip.Text.ToString();
                    queryAddress.Append(zip);
                }

                // pass the url with the query string to web browser control 

                webBrowser1.Navigate(queryAddress.ToString());
            }
            catch (Exception ex)
            {


                MessageBox.Show(ex.Message.ToString(), "Unable to Retrieve Map");
            }
        }


        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }

        private void tabPage16_Click(object sender, EventArgs e)
        {

        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            string[] s = new string[4];
            s[0]="";
            s[0]="";
            s[0] = "";
            s[0] = "";
            smK_EditListView1.Items.Add(new ListViewItem(s));  
        }

        private void BtnNext_Click(object sender, EventArgs e)
        {
            //if (gridMeasurements.Rows.Count > 0)
            //{
            //    for (int i = 0; i < gridMeasurements.Rows.Count; i++)
            //    {
            //        if (gridMeasurements.Rows[i].Cells[3].Value.ToString() != "")
            //        {
            //            objProjectEntities.Action = 0;
            //            objProjectEntities.MeasurementID = Convert.ToInt16(gridMeasurements.Rows[i].Cells[3].Value.ToString());
            //            objProjectEntities.Description = gridMeasurements.Rows[i].Cells[0].Value.ToString();

            //            if (gridMeasurements.Rows[i].Cells[1].Value.ToString() == "")
            //            { objProjectEntities.Measurement = 0; }
            //            else
            //            {
            //                objProjectEntities.Measurement = Convert.ToInt16(gridMeasurements.Rows[i].Cells[1].Value.ToString());
            //            }

            //            objProjectEntities.MeasurementUnits = gridMeasurements.Rows[i].Cells[2].Value.ToString();

            //            objProjectDB.ManageMeasurements(objProjectEntities);
            //        }
            //    }
            //}
            //if (smK_EditListView1.Items.Count > 0)
            //{
            //    //objProjectDB.DeleteMeasurements(objProjectEntities);
            //    for (int i = 0; i < smK_EditListView1.Items.Count; i++)
            //    {
            //        if (smK_EditListView1.Items[i].SubItems[3].Text != "")
            //        {
            //            objProjectEntities.Action = 0;
            //            objProjectEntities.MeasurementID = Convert.ToInt16(smK_EditListView1.Items[i].SubItems[3].Text);
            //            objProjectEntities.Description = smK_EditListView1.Items[i].SubItems[0].Text;

            //            if (smK_EditListView1.Items[i].SubItems[1].Text.Trim() == "")
            //            { objProjectEntities.Measurement = 0; }
            //            else
            //            {
            //                objProjectEntities.Measurement = Convert.ToInt16(smK_EditListView1.Items[i].SubItems[1].Text);
            //            }

            //            objProjectEntities.MeasurementUnits = smK_EditListView1.Items[i].SubItems[2].Text;

            //            objProjectDB.ManageMeasurements(objProjectEntities);
            //        }
            //        else
            //        {
            //            objProjectEntities.Action = 1;
            //            objProjectEntities.MeasurementID = 0;
            //            objProjectEntities.Description = smK_EditListView1.Items[i].SubItems[0].Text;

            //            if (smK_EditListView1.Items[i].SubItems[1].Text.Trim() == "")
            //            { objProjectEntities.Measurement = 0; }
            //            else
            //            {
            //                objProjectEntities.Measurement = Convert.ToInt16(smK_EditListView1.Items[i].SubItems[1].Text);
            //            }

            //            objProjectEntities.MeasurementUnits = smK_EditListView1.Items[i].SubItems[2].Text;

            //            objProjectDB.ManageMeasurements(objProjectEntities);
            //        }
                    
            //    }
                
                
            //}
        tab1.SelectedIndex = 6;

        }

       

        private void btnDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gridMeasurements.Rows.Count; i++)
            {
                if (gridMeasurements.Rows[i].Selected == true)
                {
                    SqlCeConnection cn = new SqlCeConnection(objManageItems.GetConnectionString());
                    SqlCeCommand cmd = new SqlCeCommand("Update Measurements set IsDeleted=1 where MeasurementID=" +Convert.ToInt16(gridMeasurements.Rows[i].Cells[3].Value.ToString()), cn);
                    cn.Open();
                    cmd.ExecuteNonQuery();
                    cn.Close();
                }
            }
            BindMeasurementstoGrid();
            
            //for (int i = 0; i < smK_EditListView1.Items.Count; i++)
            //{
            //    if (smK_EditListView1.Items[i].Checked == true)
            //    {
            //        smK_EditListView1.Items[i].Remove();
            //    }
            //}
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void txtDnyEnter(object sender, MouseEventArgs e)
        {
            if (txtLbl1.Visible != true)
            {
                DnyLbl1.Visible = false;
                DnyLbl2.Visible = false;
                DnyLbl3.Visible = false;
                DnyLbl4.Visible = false;
                DnyLbl5.Visible = false;
                txtLbl1.Visible = true;
                txtLbl2.Visible = true;
                txtLbl3.Visible = true;
                txtLbl4.Visible = true;
                txtLbl5.Visible = true;
                txtLbl1.Text = DnyLbl1.Text;
                txtLbl2.Text = DnyLbl2.Text;
                txtLbl3.Text = DnyLbl3.Text;
                txtLbl4.Text = DnyLbl4.Text;
                txtLbl5.Text = DnyLbl5.Text;
            }
        }

        private void txtDnyLeave(object sender, MouseEventArgs e)
        {
            DnyLbl1.Visible = true;
            DnyLbl2.Visible = true;
            DnyLbl3.Visible = true;
            DnyLbl4.Visible = true;
            DnyLbl5.Visible = true;
            txtLbl1.Visible = false;
            txtLbl2.Visible = false;
            txtLbl3.Visible = false;
            txtLbl4.Visible = false;
            txtLbl5.Visible = false;
            DnyLbl1.Text = txtLbl1.Text;
            DnyLbl2.Text = txtLbl2.Text;
            DnyLbl3.Text = txtLbl3.Text;
            DnyLbl4.Text = txtLbl4.Text;
            DnyLbl5.Text = txtLbl5.Text;
        }
        private ListViewItem li;
        private int X = 0;
        private int Y = 0;
        private string subItemText;
        private int subItemSelected = 0; 
        private void editListView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                //int nStart = X;
                //int spos = 0;
                //int epos = editListView1.Columns[0].Width;// .Columns[0].Width;
                //for (int i = 0; i < editListView1.Columns.Count; i++)
                //{
                //    if (nStart > spos && nStart < epos)
                //    {
                //        subItemSelected = i;
                //        break;
                //    }

                //    spos = epos;
                //    epos += editListView1.Columns[i].Width;
                //}

                // Console.WriteLine("SUB ITEM SELECTED = " + li.SubItems[subItemSelected].Text);
                //subItemText = editListView1.SubItems[subItemSelected].Text;

                string colName = editListView1.Columns[1].Text;
                //string colName = editListView1.Columns[subItemSelected].Text;
                if (colName == "Desc")
                {
                    frmNote objnote = new frmNote();
                    objnote.ShowDialog();
                }
                else
                {
                    //Rectangle r = new Rectangle(spos, li.Bounds.Y, epos, li.Bounds.Bottom);
                    //editBox.Size = new System.Drawing.Size(epos - spos, li.Bounds.Bottom - li.Bounds.Top);
                    //editBox.Location = new System.Drawing.Point(spos, li.Bounds.Y);
                    //editBox.Show();
                    //editBox.Text = subItemText;
                    //editBox.SelectAll();
                    //editBox.Focus();
                }
            }
            catch
            {
            }
            
           
        }

        private void editListView1_Click(object sender, EventArgs e)
        {
            
            //for (int i = 0; i < editListView1.Items.Count; i++)
            //{
            //    if (editListView1.Items[i].SubItems[3].Checked == true)
            //    {
            //        frmNote objnote = new frmNote();
            //        objnote.ShowDialog();
            //    }
                 
            //}
        }

        private void editListView1_ColumnClickedEvent(object source, GlacialComponents.Controls.ClickEventArgs e)
        {
            
            if (e.ColumnIndex == 3)
            {
                frmNote objnote = new frmNote();
                objnote.ShowDialog();
            }
        }

        private void editListView1_TextChanged(object sender, EventArgs e)
        {
            MessageBox.Show("hi");
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void editListView1_MouseClick(object sender, MouseEventArgs e)
        {
            //MessageBox.Show(e.Location.ToString());
            frmSelectItems objSelectItems = new frmSelectItems();
            objSelectItems.ShowDialog();
            
        }

        private void editListView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            
            MessageBox.Show(e.Location.ToString());
        }

        private void listAssemblies_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnAddNewAssembly_Click(object sender, EventArgs e)
        {
            btnSaveAssembly.Text = "Save";
            btnSaveAssembly.Visible = true;
            btnCancel1.Visible = true;
            txtAddAssembly.Visible = true;
            txtAddAssembly.Focus();
            btnEditAssembly.Enabled = false;
        }

        private void btnSaveAssembly_Click(object sender, EventArgs e)
        {
            if (txtAddAssembly.Text == "")
            {
                MessageBox.Show("Please enter a Assembly Name or click Cancel");
                return;
            }
            else if (btnSaveAssembly.Text != "Update")
            {
                objEntities.Action = 1;
                objEntities.AssemblyID = 0;
                objEntities.AssemblyName = txtAddAssembly.Text;
                objManageItems.GetAssemblies(objEntities);
                btnSaveAssembly.Visible = false;
                btnCancel1.Visible = false;
                txtAddAssembly.Visible = false;
                txtAddAssembly.Text = "";
            }
            else
            {
                objEntities.Action = 2;
                objEntities.AssemblyID = AssemblyID1;
                objEntities.AssemblyName = txtAddAssembly.Text;
                objManageItems.GetAssemblies(objEntities);
                btnSaveAssembly.Visible = false;
                btnCancel1.Visible = false;
                txtAddAssembly.Visible = false;
                txtAddAssembly.Text = "";
            }
            GetAssemblies();
            BindAssemblies();
            btnEditAssembly.Enabled = true;
            btnAddNewAssembly.Enabled = true;
        }

        private void btnCancel1_Click(object sender, EventArgs e)
        {
            btnSaveAssembly.Visible = false;
            btnCancel1.Visible = false;
            txtAddAssembly.Visible = false;
            btnAddNewAssembly.Enabled = true;
            btnEditAssembly.Enabled = true;
        }

        private void btnEditAssembly_Click(object sender, EventArgs e)
        {
            //btnSaveAssembly.Visible = true;
            //btnCancel1.Visible = true;
            //txtAddAssembly.Visible = true;
            //txtAddAssembly.Focus();
            btnSaveAssembly.Text = "Update";
            string chk = "";
            for (int j = 0; j < listAssemblies.Items.Count; j++)
            {
                if (listAssemblies.Items[j].Checked == true)
                {
                    chk = "1";
                }
            }
            if (chk == "")
            {
                MessageBox.Show("Please select an Assembly to edit");
                return;
            }
            else
            {
                txtAddAssembly.Visible = true;
                btnSaveAssembly.Visible = true;
                btnCancel1.Visible = true;
                for (int i = 0; i < listAssemblies.Items.Count; i++)
                {
                    if (listAssemblies.Items[i].Checked == true)
                    {
                        txtAddAssembly.Text = listAssemblies.Items[i].Text;
                        AssemblyID1 = Convert.ToInt16(listAssemblies.Items[i].SubItems[1].Text);
                    }
                }
            }
        }

        private void btnAssignAssemblies_Click(object sender, EventArgs e)
        {
            string chk = "";
            for (int j = 0; j < listItemsAss.Items.Count; j++)
            {
                if (j != 0)
                {
                    if (listItemsAss.Items[j].Checked == true)
                    {
                        chk = "1";
                    }
                }
            }
            if (chk == "")
            {
                MessageBox.Show("Please select an Item to assign");
                return;
            }
            else
            {
                for (int i = 0; i < listItemsAss.Items.Count; i++)
                {
                    if (listItemsAss.Items[i].Checked == true)
                    {
                        SqlCeConnection cn = new SqlCeConnection(objManageItems.GetConnectionString());
                        SqlCeCommand cmd = new SqlCeCommand("update Items set AssemblyID=" + Convert.ToInt16(ddlAssemblies.SelectedValue.ToString()) + " where ItemID=" + Convert.ToInt16(listItemsAss.Items[i].SubItems[3].Text), cn);
                        cn.Open();
                        cmd.ExecuteNonQuery();
                        cn.Close();
                    }
                }
                MessageBox.Show("Items Successfully Assigned to the Selected Assembly");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            frmConcreteCalculator obj = new frmConcreteCalculator();
            obj.ShowDialog();
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            MessageBox.Show(e.ColumnIndex.ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            objEntities.Action = 0;
            ds = objManageItems.GetItems(objEntities);
            //dataGridView2.DataSource = ds.Tables[0];
            DataGridViewColumn obj = new DataGridViewColumn();
            obj.HeaderText = "Col1";
            obj.DataPropertyName = "ItemName";
            
            
            gridMeasurementChild.Columns.Add("Col1","ItemName");
            ArrayList arr=new ArrayList();
            
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                
                arr.Add(ds.Tables[0].Rows[i]["ItemName"].ToString());
                //dataGridView2.AutoResizeColumn(0);// = ds.Tables[0].Rows[i]["ItemName"].ToString();
                
            }
            gridMeasurementChild.DataSource = arr;
            gridMeasurementChild.AutoResizeColumn(0);

            
            objEntities.Action = 0;
            ds = objManageItems.GetItems(objEntities);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                ListViewItem lv = new ListViewItem(ds.Tables[0].Rows[i]["ItemName"].ToString());
                lv.SubItems.Add(ds.Tables[0].Rows[i]["ItemID"].ToString());
                ///listView5.Items.Add(lv);
            }
        }

        private void listView5_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            DataSet ds = new DataSet();
            objEntities.Action = 0;
            ds = objManageItems.GetItems(objEntities);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            { 
                ListViewItem lv=new ListViewItem(ds.Tables[0].Rows[i]["ItemName"].ToString());
                lv.SubItems.Add(ds.Tables[0].Rows[i]["ItemID"].ToString());
                //listView5.Items.Add(lv);
            }
            MessageBox.Show(e.Column.ToString());
        }

        private void dataGridView2_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >-1)
            {
                if (e.ColumnIndex == 0)
                {
                    string lbl = gridMeasurementChild.Rows[e.RowIndex].Cells["ItemID"].Value.ToString();
                    if (lbl == "0")
                    {
                        ProjectDB.MeasurementID = Convert.ToInt16(gridMeasurementChild.Rows[e.RowIndex].Cells["MeasurementID"].Value.ToString());
                        frmSelectItems objSelectItems = new frmSelectItems();
                        objSelectItems.FormClosed+=new FormClosedEventHandler(objSelectItems_FormClosed);
                        objSelectItems.ShowDialog();

                    }
                    else if (lbl != "0")
                    {
                        ProjectDB.ID = Convert.ToInt16(gridMeasurementChild.Rows[e.RowIndex].Cells["ID"].Value.ToString());
                        ProjectDB.MeasurementID = Convert.ToInt16(gridMeasurementChild.Rows[e.RowIndex].Cells["MeasurementID"].Value.ToString());
                        frmLineItemData objLineItemData = new frmLineItemData();
                        objLineItemData.FormClosed += new FormClosedEventHandler(objSelectItems_FormClosed);
                        objLineItemData.ShowDialog();
                    }
                }

                if (e.ColumnIndex == 2)
                {
                    if (e.Button.ToString() == "Right")
                    {
                            ProjectDB.MeasurementID = Convert.ToInt16(gridMeasurementChild.Rows[e.RowIndex].Cells["MeasurementID"].Value.ToString());
                        
                            ProjectDB.ID = Convert.ToInt16(gridMeasurementChild.Rows[e.RowIndex].Cells["ID"].Value.ToString());
                        frmRoofCalculator objRoofCalculator = new frmRoofCalculator();
                        objRoofCalculator.FormClosed += new FormClosedEventHandler(objSelectItems_FormClosed);
                        objRoofCalculator.ShowDialog();
                    }
                }

                if (e.ColumnIndex == 3)
                {
                    if (gridMeasurementChild.Rows[e.RowIndex].Cells["ID"].Value.ToString() != "0")
                    {
                        ProjectDB.MeasurementID = 0;
                        ProjectDB.ID = Convert.ToInt16(gridMeasurementChild.Rows[e.RowIndex].Cells["ID"].Value.ToString());
                        ProjectDB.Desctiption = gridMeasurementChild.Rows[e.RowIndex].Cells["Description"].Value.ToString();
                    }
                    else
                    {
                        ProjectDB.ID = 0;
                        ProjectDB.MeasurementID = Convert.ToInt16(gridMeasurementChild.Rows[e.RowIndex].Cells["MeasurementID"].Value.ToString());
                    }
                    frmNote objNote = new frmNote();
                    objNote.ShowDialog();
                }

                if (e.ColumnIndex == 6)
                {
                    if (e.Button.ToString() == "Right")
                    {
                        if (gridMeasurementChild.Rows[e.RowIndex].Cells["ID"].Value.ToString() != "0")
                        {
                            ProjectDB.ID = Convert.ToInt16(gridMeasurementChild.Rows[e.RowIndex].Cells["ID"].Value.ToString());
                            frmConcreteCalculator objConcreteCalculator = new frmConcreteCalculator();
                            objConcreteCalculator.FormClosed += new FormClosedEventHandler(objSelectItems_FormClosed);
                            objConcreteCalculator.ShowDialog();
                        }
                    }
                }

            }
        }
        private void objSelectItems_FormClosed(object sender, FormClosedEventArgs e)
        {
            BindDatatoWorkSheet();
        }

        private void txtMaterialPercentage_Leave(object sender, EventArgs e)
        {
            if (txtMaterialPercentage.Text != "")
            {
                decimal percentage = Convert.ToDecimal(txtMaterialPercentage.Text) / 100;
                decimal percentagetotal = Convert.ToDecimal(resTotalMaterial.Text) * percentage;
                lblMaterialSubtotal.Text = Convert.ToString(Convert.ToDecimal(resTotalMaterial.Text) + percentagetotal);

                decimal grandsubtotal = Convert.ToDecimal(lblMaterialSubtotal.Text) + Convert.ToDecimal(lblLaborSubtotal.Text);
                lblGrandSubtotal.Text = grandsubtotal.ToString();
                lblFinalSubTotal.Text = lblGrandSubtotal.Text;
                lblBID.Text = "$" + lblFinalSubTotal.Text;
            }
            
        }

        private void txtLabourPercentage_Leave(object sender, EventArgs e)
        {
            if (txtLabourPercentage.Text != "")
            {
                decimal percentage = Convert.ToDecimal(txtLabourPercentage.Text) / 100;
                decimal percentagetotal = Convert.ToDecimal(resTotalLabor.Text) * percentage;
                lblLaborSubtotal.Text = Convert.ToString(Convert.ToDecimal(resTotalLabor.Text) + percentagetotal);

                decimal grandsubtotal = Convert.ToDecimal(lblMaterialSubtotal.Text) + Convert.ToDecimal(lblLaborSubtotal.Text);
                lblGrandSubtotal.Text = grandsubtotal.ToString();
                lblFinalSubTotal.Text = lblGrandSubtotal.Text;
                lblBID.Text = "$" + lblFinalSubTotal.Text;
            }
        }

        private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            
        }

        private void dataGridView3_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            string MiscItemName = dataGridView3.Rows[e.RowIndex].Cells[0].Value.ToString();
            string MiscItemCost = dataGridView3.Rows[e.RowIndex].Cells[1].Value.ToString();
            string MiscItemID = dataGridView3.Rows[e.RowIndex].Cells[2].Value.ToString();

            objProjectEntities.MiscItemName = MiscItemName;
            objProjectEntities.MiscItemCost = MiscItemCost;

            if (MiscItemID == "")
            {
                objProjectEntities.MiscItemID = 0;
            }
            else
            {
                objProjectEntities.MiscItemID =Convert.ToInt16(MiscItemID);
            }
            objProjectDB.ManageMiscItems(objProjectEntities);
            BindMiscItemstoGrid();
        }

        private void dataGridView3_CellClick(object sender, DataGridViewCellEventArgs e)
        {
        //    if (e.ColumnIndex == 0)
        //    {
        //        DataGridViewComboBoxCell dc = new DataGridViewComboBoxCell();
        //        dc.Items.Add("asdf");
        //        dataGridView3.Rows[e.RowIndex].Cells.Insert(0, dc);
        //    }
            


        }

        private void resTotalLabor_Click(object sender, EventArgs e)
        {

        }

        private void txtProfitMargin_Leave(object sender, EventArgs e)
        {
            if (txtProfitMargin.Text != "")
            {
                decimal res = Convert.ToDecimal(lblFinalSubTotal.Text) * Convert.ToDecimal(txtProfitMargin.Text);
                res = res / 100;
                res = decimal.Round(res, 3);
                lblBID.Text = Convert.ToString(res + Convert.ToDecimal(lblFinalSubTotal.Text));
            }
        }

        private void ddlCostPer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCostPer.SelectedIndex != -1)
            {
                radioButton2.Text = ddlCostPer.SelectedItem.ToString();
                radioButton2.Checked = true;
            }
            else
            {
                radioButton1.Checked = true;
            }
        }

        private void gridMeasurements_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //if (DataBinding != 0)
            //{
            string Description = "";
            string Measurement = "";
            string MeasurementUnits = "";
            string MeasurementID = "";
            DataSet ds = new DataSet();
            ds = objProjectDB.GetMeasurements(objProjectEntities);
            if (ds.Tables[0].Rows.Count > 0)
            {
                Description = gridMeasurements.Rows[e.RowIndex].Cells[0].Value.ToString();
                Measurement = gridMeasurements.Rows[e.RowIndex].Cells[1].Value.ToString();
                MeasurementUnits = gridMeasurements.Rows[e.RowIndex].Cells[2].Value.ToString();
                MeasurementID = gridMeasurements.Rows[e.RowIndex].Cells[3].Value.ToString();
                
            }
            else
            {
                Description = gridMeasurements.Rows[e.RowIndex].Cells[0].Value.ToString();
                Measurement = "";
                MeasurementUnits = "";
                MeasurementID = "";
            }
            string column = e.ColumnIndex.ToString();
            
            if (MeasurementID != "")
            {
                objProjectEntities.Action = 0;
                objProjectEntities.MeasurementID = Convert.ToInt16(MeasurementID);
                objProjectEntities.Description = Description;

                if (Measurement == "")
                { objProjectEntities.Measurement = 0; }
                else
                {
                    objProjectEntities.Measurement = Convert.ToInt16(Measurement);
                }

                objProjectEntities.MeasurementUnits = MeasurementUnits;

                objProjectDB.ManageMeasurements(objProjectEntities);
            }
            else
            {
                objProjectEntities.Action = 1;
                objProjectEntities.MeasurementID = 0;
                objProjectEntities.Description = Description;

                if (Measurement == "")
                { objProjectEntities.Measurement = 0; }
                else
                {
                    objProjectEntities.Measurement = Convert.ToInt16(Measurement);
                }

                objProjectEntities.MeasurementUnits = MeasurementUnits;

                objProjectDB.ManageMeasurements(objProjectEntities);
            }
            BindMeasurementstoGrid();
            //}
            
        }

        private void txtCoverage_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button.ToString() == "Left")
            {
                frmConcreteCalculator objConcreteCalcualtor = new frmConcreteCalculator();
                objConcreteCalcualtor.FormClosed += new FormClosedEventHandler(objConcreteCalcualtor_FormClosed);
                objConcreteCalcualtor.ShowDialog();
                this.Refresh();
                txtCoverage.Refresh();
            }
        }

        private void objConcreteCalcualtor_FormClosed(object sender, FormClosedEventArgs e)
        {
            txtCoverage.Text = ProjectDB.Coverage;
            ProjectDB.Coverage = "";
            BindDatatoWorkSheet();
        }












        //        GlacialList mylist = new GlacialList();

        //mylist.Columns.Add( "Column1", 100 ); // this can also be added 
        //         // through the design time support 
        //mylist.Columns.Add( "Column2", 100 ); 
        //mylist.Columns.Add( "Column3", 100 ); 
        //mylist.Columns.Add( "Column4", 100 ); 

        //GLItem item;

        //item = this.glacialList1.Items.Add( "Atlanta Braves" );
        //item.SubItems[1].Text = "8v";
        //item.SubItems[2].Text = "Live";
        //item.SubItems[2].BackColor = Color.Bisque;
        //item.SubItems[3].Text = "MLB.TV"; 

        //item = this.glacialList1.Items.Add( "Florida Marlins" );
        //item.SubItems[1].Text = "";
        //item.SubItems[2].Text = "Delayed";
        //item.SubItems[2].BackColor = Color.LightCoral;
        //item.SubItems[3].Text = "Audio";


        //item.SubItems[1].BackColor = Color.Aqua; // set the background 
        //      // of this particular subitem ONLY
        //item.UserObject = myownuserobjecttype; // set a private user object
        //item.Selected = true; // set this item to selected state
        //item.SubItems[1].Span = 2; // set this sub item to span 2 spaces

        //ArrayList selectedItems = mylist.SelectedItems; 


    }
}
